package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Goal;
import com.entity.Project;
import com.util.DBHelp;
import com.util.RowMapper;

public class GoalDao {

	private DBHelp<Goal> db=new DBHelp<Goal>();
	
	public void save(Goal goal){
		String sql = "INSERT INTO t_goal(id,goalname,goalcontent,createtime,projectid,userid) values(?,?,?,?,?,?)";
		db.executeSQL(sql, goal.getId(),goal.getGoalName(),goal.getGoalContent(),goal.getCreateTime(),goal.getProjectid(),goal.getUserid());
	}
	
	public List<Goal> findByProjectId(String projectid){
		String sql = "select id,goalname,goalcontent,createtime,projectid,userid from t_goal where projectid=?";
		return db.executeQueryForList(sql, new GoalRowMapper(),projectid);
	}
	
	public boolean update(Goal goal) {
		String sql = "update t_goal set goalname=? goalcontent=?  where id = ?";
		return db.executeSQL(sql,goal.getGoalName(),goal.getGoalContent(),goal.getId());
	}
	
	
	private class GoalRowMapper implements RowMapper<Goal>{

		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal=new Goal();
			
			goal.setId(rs.getString("id"));
			goal.setCreateTime(rs.getString("createtime"));
			goal.setGoalContent(rs.getString("goalcontent"));
			goal.setGoalName(rs.getString("goalname"));
			goal.setUserid(rs.getString("userid"));
			goal.setProjectid(rs.getString("projectid"));
			
			return goal;
		}
	}

	public Goal findById(String goalId){
		// TODO Auto-generated method stub
		String sql = "SELECT id,goalname,goalcontent,createtime,projectid,userid FROM t_goal WHERE id = ?";
		return db.executeQueryForObject(sql, new GoalRowMapper(), goalId);
	}
	
	
}
